﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SequenceCalculation
{
    class SequenceCalculation
    {
        static double CurrentMember1(int i)
        {
            return 1 / (Math.Pow(2, i));
        }

  
        static double CurrentMember2(int i)
        {
            double res = 1;
            while (i > 0)
            {
                res /= i;
                i--;
            }
            return res;
        }

        static double CurrentMember3(int i)
        {
            return 1 / (Math.Pow(2, i - 1))*Math.Pow(-1, i);
        }

        public delegate double CalculateSumDelegate(int n);

        public static double CalculateSum()
        {
            CalculateSumDelegate d = new CalculateSumDelegate(CurrentMember3);
        
            double result = 0;
            double currentMember = 1;
            double previousMember = 0;
            int i = 2;

            while(Math.Abs(currentMember - previousMember) > 0.000001)
            {
                result += currentMember;
                previousMember = currentMember;
                currentMember = d(i);
                Console.WriteLine(currentMember);
                i++;
            }
            return result;
        }

        static void Main(string[] args)
        {
            double sum = CalculateSum();
            Console.WriteLine("{0:F2}", sum);
        }
    }
}
